import { defineStore } from 'pinia'
import { ref } from 'vue'
import request from '../utils/request'

export const useActivityStore = defineStore('activity', () => {
  const activities = ref([])
  const types = ref([])
  const loading = ref(false)
  const total = ref(0)

  const fetchActivities = async (params = {}) => {
    loading.value = true
    try {
      const response = await request.get('/activities', { params })
      activities.value = response.data.data
      total.value = response.data.total
    } finally {
      loading.value = false
    }
  }

  const fetchTypes = async () => {
    try {
      const response = await request.get('/activity-types')
      types.value = response.data
    } catch (error) {
      console.error('获取活动类型失败:', error)
    }
  }

  const createActivity = async (data) => {
    const response = await request.post('/activities', data)
    return response.data
  }

  const updateActivity = async (id, data) => {
    const response = await request.put(`/activities/${id}`, data)
    return response.data
  }

  const deleteActivity = async (id) => {
    await request.delete(`/activities/${id}`)
  }

  const getActivity = async (id) => {
    const response = await request.get(`/activities/${id}`)
    return response.data
  }

  return {
    activities,
    types,
    loading,
    total,
    fetchActivities,
    fetchTypes,
    createActivity,
    updateActivity,
    deleteActivity,
    getActivity
  }
}) 